﻿using System;
using System.Windows;
using PersonalHealth.Data.Model;
using PersonalHealth.Data.IService;
using PersonalHealth.Data.Service;
using UI.Comm.Controls;
using UI.Comm.Common;
using HealthManagement.Common;

namespace HealthManagement.Views
{
    /// <summary>
    /// DoctorForm.xaml 的交互逻辑
    /// </summary>
    public partial class DoctorForm : PMWindow
    {
        Doctor user = new Doctor() { Id = 0 };
        public event Action<int> onSaveHandler;
        public DoctorForm()
        {
            InitializeComponent();

            this.DataContext = user;
        }

        public DoctorForm(Action<int> a)
            : this()
        {
            onSaveHandler = a;

        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            IUserService ser = new UserService();

            if (string.IsNullOrEmpty(tbPsd.Password))
            {
                tbPsd.Password = "";
            }
            if (string.IsNullOrEmpty(tbPsdConfirm.Password))
            {
                tbPsdConfirm.Password = "";
            }
            if (tbPsd.Password != tbPsdConfirm.Password)
            {
                MessageBox.Show("密码输入不一致请重新输入！");
                tbPsd.Clear();
                tbPsdConfirm.Clear();
                tbPsd.Focus();
                return;
            }
            user.Password = tbPsd.Password;

            if (user.Id == 0)
            {
                if (string.IsNullOrEmpty(user.UserName))
                    user.UserName = "匿名用户";

                user.UserTypeId = (int)UserType.Doctor;
                user.CreateDate = user.ModifyDate = DateTime.Now;
                ser.Register(user);

                Tools.ShowMessage(true, string.Format("新增成功，服务编号为：{0}", user.ServiceNo));
            }
            else
            {
                user.ModifyDate = DateTime.Now;
                ser.Update(user);
            }
            this.Close();
            if (onSaveHandler != null)
                onSaveHandler(user.Id);
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

    }
}
